import managerDao from "../dao/managerDB.js";
import {getUniAccount, httpResult} from "../utils/utils.js";
//注册
export const signIn = async (req, res, next) => {
    let { passwordMd5, role } = req.body;
    role = role || 0;
    if(!passwordMd5) {
        throw new Error("密码不能为空！");
        return;
    }
    let account = await getUniAccount();
    let manager = await managerDao.addManager(account, passwordMd5, role);
    if(!manager) {
        throw new Error("注册失败请联系管理员！");
        return;
    }
    res.json(httpResult.success("注册成功！"));
};

//登录
export const login = async (req, res, next) => {
    let { account, passwordMd5 } = req.body;
    if( !(account && passwordMd5) ) {
        throw new Error("账号密码不能为空！");
        return;
    }
    let isExisted = await managerDao.isExistedByPassword(account, passwordMd5);
    if(isExisted) {
        res.json(httpResult.success("登录成功！"));
        return;
    }
    throw new Error("账号密码错误！");
};

//获取管理员权限
export const getRole = async (req, res, next) => {
    let { account } = req.query;
    if(!account) {
        throw new Error("请输入管理员账号！");
        return;
    }
    let isExisted = await managerDao.isExisted(account);
    if(!isExisted) {
        throw new Error('请输入正确的管理员账号！');
        return;
    }
    let roles = await managerDao.getManagerRole(account);
    res.json(roles);
};

//添加管理员权限

//删除管理员权限

